﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace X23.Extensions.MiddlewareExtensions
{
    //Middleware
    //推荐用工厂模式 强类型 【注意：需要注册工厂】
    //ASP.NET Core 中间件 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0
    //写入自定义 ASP.NET Core 中间件 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/write?view=aspnetcore-5.0
    //ASP.NET Core 中基于工厂的中间件激活 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-5.0

    //Filter
    //https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/filters?view=aspnetcore-5.0

    public static class AllMiddlewareExtensions
    {
        #region MyRegion
        /// <summary>
        /// 注册 异常处理中间件
        /// </summary>
        /// <param name="services"></param>
        public static void AddErrorHandlingMiddleware(this IServiceCollection services)
        {
            services.AddTransient<ErrorHandlingMiddleware>();
        }
        /// <summary>
        /// 使用 异常处理中间件
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseErrorHandlingMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<ErrorHandlingMiddleware>();
        }
        #endregion
    }
}
